var conferenceApi = {
    baseURI:null,
    auth:null,
    protocol: 'http',

    _init(protocol, host, auth) {
        this.baseURI = protocol + '://' + host
        this.auth = auth
        this.protocol = protocol
    }
}

conferenceApi.queryConferenceRooms = function (userId) {
    return new Promise((resolve, reject) => {
        let data = {userId: userId}
        $.ajax({
            url: this.baseURI + '/control/20211012/infs/getConfoList.json',
            type: 'post',
            dataType: 'json',
            data: JSON.stringify(data),
            contentType: 'application/json',
            success: function (data) {
                resolve(data)
            },
            error: function (error) {
                reject(error)
            }
        })
    })
}

conferenceApi.queryCameraUrlByName = function (name) {
    return new Promise((resolve, reject) => {
        $.ajax({
            url:this.baseURI+'/video-service/api-v1.0/Camera/queryUrlByName?name='+name,
            type:'get',
            dataType:'json',
            contentType:'application/json',
            success:function (data) {
                resolve(data)
            },
            error:function (error) {
                reject(error)
            }
        })
    })
}

conferenceApi.queryConferenceMembers = function (confNo, confName) {
    return new Promise((resolve, reject) => {
        $.ajax({
            url: this.baseURI + '/system-service/api-v2.0/conference/members?confId=' + confNo + '&confName=' + confName,
            type: 'get',
            dataType: 'json',
            contentType: 'application/json',
            headers: {
                'Authorization': localStorage.getItem('rh_token')
            },
            success: function (data) {
                resolve(data)
            },
            error: function (error) {
                reject(error)
            }
        })
    })
}

conferenceApi.queryTempConferenceMembers = function (confName) {
    return new Promise((resolve, reject) => {
        let data = {confName}
        $.ajax({
            url: this.baseURI + '/system-service/api-v2.0/conference/temp/members',
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            data: JSON.stringify(data),
            headers: {
                'Authorization': this.auth
            },
            success: function (data) {
                resolve(data)
            },
            error: function (error) {
                reject(error)
            }
        })
    })
}

conferenceApi.queryVcsConferenceMembers = function (data) {
    return new Promise((resolve, reject) => {
        $.ajax({
            url: this.baseURI + '/system-service/api-v2.0/conference/vcs/members',
            data: JSON.stringify(data),
            type: 'post',
            contentType: 'application/json',
            headers: {
                'Authorization': localStorage.getItem('rh_token')
            },
            success: function (data) {
                resolve(data)
            },
            error: function (error) {
                reject(error)
            }
        })
    })
}

conferenceApi.queryVcsMembers = function (protocol, ip, port, key) {
    if (!protocol) {
        protocol = this.protocol
    }
    if (!ip) {
        ip = this.host
    }
    let baseUri = protocol + '://' + ip
    port ? baseUri = baseUri + ':' + port + '/' : baseUri = baseUri + '/'
    return new Promise((resolve, reject) => {
        $.ajax({
            url: baseUri + '/video-conference-system/vcs/public/meeting/members?secretKey=' + key,
            type: 'get',
            contentType: 'application/json',
            dataType: 'json',
            success:function (data) {
                resolve(data)
            },
            error:function (error) {
                reject(error)
            }
        })
    })
}

conferenceApi.queryUserConfig = function (userId,full) {
    return new Promise((resolve, reject) => {
        $.ajax({
            url: this.baseURI + '/control/api_v2.2.5/userConfig/getUserConfig.json',
            data:JSON.stringify({userId:userId}),
            dataType:'json',
            type:'post',
            contentType:'application/json',
            success:function (data) {
                let config = {}
                if (data.code == 200) {
                    if (data.data) {
                        config.cameraId = data.data.cameraId
                        config.speakId = data.data.speakId
                        config.micId = data.data.micId
                    }
                }
                if (full) {
                    resolve(data.data)
                }else{
                    resolve(config)
                }
            },
            error: function (error) {
                console.error(error)
                reject(error)
            }
        })
    })
}

conferenceApi.queryConfoList = function (userId) {
    return new Promise((resolve, reject) => {
        $.ajax({
            url: this.baseURI + '/control/20211012/infs/getConfoList.json',
            data:JSON.stringify({userId:userId}),
            dataType:'json',
            type:'post',
            contentType:'application/json',
            success:function (data) {
				if(data.apiResult){
					resolve(data.apiResult.data)
				}
            },
            error: function (error) {
                console.error(error)
                reject(error)
            }
        })
    })
}

conferenceApi.saveUserConfig = function (data) {
    let userId = data.userId
    if (!userId) {
        return null
    }
    delete data.userId
    this.queryUserConfig(userId,true).then(config=>{
        Object.assign(config,data)
        let _data = {configValue:config,userId:userId, type: '0'}
        return new Promise((resolve, reject) => {
            $.ajax({
                url: this.baseURI + '/control/api_v2.2.5/userConfig/updateUserConfig.json',
                data: JSON.stringify(_data),
                dataType: 'json',
                contentType:'application/json',
                type:'post',
                success:function (data) {
                    resolve(data)
                },
                error:function (error) {
                    reject(error)
                }
            })
        })
    })


}